/**
 * 
 */
$(inittalking());
function inittalking(){
	$.ajax({
		type : "post",
		url : "GetCoopTalkServlet",
		data :{projID:projID,state:"",talk_id:""},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			var messNode=$("#ingTalk");
			messNode.children().remove();
			var pareNode,talkTools;
			$.each(jsondata.talkArray,function (n,v){
				if(v.State=="结束"){
					return false;
				}
				pareNode=$("<div></div>").addClass("message");
				// 讨论管理工具
				talkTools=addtalktools(v);
				pareNode.append(talkTools);
				pareNode.append("<a href=\"#\" target=\"_blank\" title=\"\"><img alt=\"\""+
										"class=\"avatar\" src=\"../userIcons/"+v.userIcon+"\"></a>"+
									"<div class=\"name\">"+
										"<a href=\"#\" data-stack-root=\"true\" data-stack=\"true\" title=\"\">"+v.userNickName+"</a>"+
									"</div>"+
									"<a data-stack=\"\" href=\"../communication/cooptalkc.html?talk={tid:"+v.ID+"}\" class=\"message-link\"> "+
									"<span class=\"message-title\"><span class=\"document-rest\" title=\""+v.Title+"\">"+v.Title+"</span></span> "+
									"<span class=\"message-content\"> "+v.Body+" </span>"+
									"</a> <span class=\"time\" title=\""+v.CreatedOn+"\""+
										"data-readable-time=\"2015-05-14T19:56:19+08:00\">"+v.CreatedOn+"</span>"+
									"<div class=\"comment-info\">"+
									"</div>");
				messNode.append(pareNode);
			});
			if(messNode.children().length==0){
				messNode.append("<div class=\"init init-discussion \"><p class=\"title\"> 还没有讨论⊙０⊙..赶快发起新的讨论吧！！！ </p></div>");
			}
			var emessNode=$("#closureTalk");
			emessNode.children().remove();
			var epareNode,etalkTools;
			$.each(jsondata.talkArray,function (n,v){
				if(v.State=="进行"){
					return false;
				}
				epareNode=$("<div></div>").addClass("message");
				// 讨论管理工具
				etalkTools=addtalktools(v);
				epareNode.append(etalkTools);
				epareNode.append("<a href=\"#\" target=\"_blank\" title=\"\"><img alt=\"\""+
										"class=\"avatar\" src=\"../userIcons/"+v.userIcon+"\"></a>"+
									"<div class=\"name\">"+
										"<a href=\"#\" data-stack-root=\"true\" data-stack=\"true\" title=\"\">"+v.userNickName+"</a>"+
									"</div>"+
									"<a data-stack=\"\" href=\"../communication/cooptalkc.html?talk={tid:"+v.ID+"}\" class=\"message-link\"> "+
									"<span class=\"message-title\"><span class=\"document-rest\" title=\""+v.Title+"\">"+v.Title+"</span></span> "+
									"<span class=\"message-content\"> "+v.Body+" </span>"+
									"</a> <span class=\"time\" title=\""+v.CreatedOn+"\""+
										"data-readable-time=\"2015-05-14T19:56:19+08:00\">"+v.CreatedOn+"</span>"+
									"<div class=\"comment-info\">"+
									"</div>");
				emessNode.append(epareNode);
			});
			if(emessNode.children().length==0){
				emessNode.append("<div class=\"init init-discussion \"><p class=\"title\"> 所有讨论都在进行 </p></div>");
			}
			
		},
		error : function() {
			// TODO do something
		}
	});
}
function addtalktools(obj){
	var parentsNode=$("<div></div>").addClass("message-actions actions").attr("data-visible-to","member");
	var contentdivNode=$("<div></div>").addClass("inr");
	var setTopNode=$("<a href=\"javascript:;\" onclick=\"sharetoBBS('"+obj.Title+"','"+obj.Body+"','"+obj.Creator+"')\" class=\"stick\" title=\"分享至论坛\" data-method=\"post\" data-remote=\"true\" data-loading=\"true\"> <i class=\"twr twr-arrow-circle-up\"></i></a>");
	if(obj.State=="进行"){
		var shareNode=$("<a href=\"javascript:;\"  onclick=\"updateTalkstate('"+obj.ID+"','结束')\" class=\"archive\" title=\"取消讨论\" data-method=\"post\" data-remote=\"true\" data-loading=\"true\"> <i class=\"twr twr-archive-custom\"></i></a>");
	}
	contentdivNode.append(setTopNode);
	contentdivNode.append(shareNode);
	parentsNode.append(contentdivNode);
	return parentsNode;
}
function updateTalkstate(t_id,tyle){
	$.ajax({
		type : "post",
		url : "UpdateCoopTalkStateServlet",
		data :{t_id:t_id,tyle:tyle},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="true"){
					inittalking();
			}else{
				confirm("操作 失败！请重试...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}
function sharetoBBS(textTitle,textArea,uid){
	$.ajax({
		type : "post",
		url : "../community/PutTopicServlet",
		data :{labelID:'0',textTitle:textTitle,textArea:textArea,c_id:'5',user_id:uid},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="0"){
				confirm("分享 成功！请重试...");
			}else{
				confirm("分享 失败！ 版主偷懒咯~~~");
			}
		},
		error : function() {
			// TODO do something
			
		}
	});
}
function addtalking(){
	var edittalkNode;
	edittalkNode=$("<div class=\"form-item\"><div class=\"form-field\" style=\"margin-bottom: 13px; margin-top: 13px;\">"+
			"<input id=\"txt-title\" type=\"text\" data-autosave=\"project-9e3709e48ef542578b3c16b8920bb1fa-new-message-title\" data-validate-msg=\"话题最长255个字符\" data-validate=\"length:0,255\" placeholder=\"话题\" name=\"subject\" tabindex=\"1\">"+
			"</div></div>"+
			"<div class=\"form-item\"><textarea id=\"edit_area\" name=\"edit_area\" rows=\"20\" style=\"width: 99.4%;height:200px\"></textarea> </div>"+
			"<div class=\"form-item notify\"><div class=\"notify-title\"></div></div>"+
			"<div class=\"form-buttons\"  style=\"margin-top: 13px; margin-bottom: 20px;\">"+
			"<button id=\"btn-post\" class=\"btn btn-primary\" data-disable-with=\"正在提交...\" onclick=\"postTalk()\" tabindex=\"1\">发起讨论</button>"+
			"<a id=\"link-cancel-post\" class=\"btn btn-x\" onclick=\"removeNodechildren('editNode')\" href=\"javascript:;\" tabindex=\"2\">取消</a>"+
			"</div>");
	$("#editNode").children().remove();
	$("#editNode").append(edittalkNode);
	$(function (){
		edittalkNode.find("#edit_area").xheditor(
				{
					plugins : getCodePlugin(),
					tools : "Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Emot,Table,Code,|,Source,Fullscreen",
					upImgUrl : "!../learning/uploadimg.jsp",
					loadCSS : "../css/editor.css"
				});
	});
}
function removeNodechildren(id){
	$("#"+id).children().remove();
	$("#"+id).append("<div class=\"editor-placeholder fake-textarea\" data-droppable=\"\" onclick=\"addtalking()\" >点击发起讨论</div>");
}
function postTalk(){
	var title=$("#txt-title").val();
	var body=$("#edit_area").val();
	$.ajax({
		type : "post",
		url : "PutNewTalkServlet",
		data :{projID:projID,title:title,body:body,user_id:user_id},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if (jsondata.isSuccess == "true") {
				if (confirm("发表 成功!")) {
					inittalking();
					removeNodechildren('editNode');
				}
			} else {
				confirm("发表 失败！请重试...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}